<?php //tailvh@gmail.com

/*
 * class: Paging 
 * use: Create link for paging
 * -> [IN] $total:   total of records
 * 		   $current: current page
 * 		   $limit:   number of rows per page
 * 		   $url: 	 current page's url need paging
 * -> [OUT]$html:	 [start] | [back] | [1] | [2] | 3 | [4] | [5] | [next] | [end]  
 * 										which link to corrent page
 */

class Paging
{
    function createPaging($total, $current, $limit, $url = '')
    {
        $numPage = floor($total/ $limit);
        
        //$total + $limit >= $numpage*$limit >= $total
        if (($total/ $limit) - $numPage > 0)
        {
            $numPage += 1;
        }
        
        ////////////////////////
        //make links for pages//
        ////////////////////////
       
        $html = '';
        
        //dont show when $total <= $limit
        if ($numPage == 1)
        {
            return '';
        }
        
        //for [START] | [BACK] buttons
        if ($current == 1)
        {
        	//if at start disable these buttons
            $html .= '       ';
            $html .= '       ';
        } else 
        {
        	//if not at end make link for these buttons
            $html .= "<a href='$url&p=1'><b>|<<</b></a> ";
            $html .= "<a href='$url&p=".($current - 1). "'><b><<</b></a>  ";
        }
        
        //for BODY buttons ... [3] | [4] | [5] ....
        if($current <=3)
        {
        	//if current at page 1 show:  1 | [2] | [3] | [4] | [5] (page <= 3 show at left)
            for($i=1; ($i<=5) and ($i <= $numPage); $i++)
            {
                if ($i == $current)
                {
                    $html .= '  &nbsp'.$i.'&nbsp  ';
                }else
                {
                    $html .= "<a href='$url&p=$i'><b>$i</b></a> ";
                }
            }
        }else
        {
        	//if current at page 5 show:  [3] | [4] | 5 | [6] | [7] ($numPage - 2>= $page > 3 show at middle)
            if ($numPage >= $current + 2)
            {
                for($i=$current-2; ($i <=$current+2) and ($i <= $numPage); $i++)
                {
                    if ($i == $current)
                    {
                    	//if is current
                        $html .=  '  &nbsp'.$i.'&nbsp  ';
                    }else
                    {
                    	//if is's current
                        $html .= "<a href='$url&p=$i'><b>$i</b></a> ";
                    }
                }
            }else
            {
            	//if current at page 6 (with 7 is $numPage) show:  [3] | [4] | [5] | 6 | [7] ($numPage - 2 < $page show at right)
                for($i = $numPage - 4;$i <= $numPage; $i++)
                {
                    if($i > 0)
                    {
                        if ($i == $current)
                        {
                        	//if is current
                            $html .=  '  &nbsp'.$i.'&nbsp  ';
                        }else
                        {
                        	//if is't current
                            $html .= "<a href='$url&p=$i'><b>$i</b></a> ";
                        }
                    }                        
                }
            }
        }
        
        //for [NEXT] | [END] button
        if ($current == $numPage || $numPage == 0 || $numPage == 1)
        {
        	//if current = end disable these buttons
            $html .= '     ';
            $html .= '     ';
        } else 
        {
        	//if current != end make link for these buttons
            $html .= "<a href='$url&p=".($current + 1). "'><b>>></b></a> ";
            $html .= "<a href='$url&p=$numPage'><b>>>|</b></a>";
        }
        return $html;
    }
} 
?>